package org.agilar.gwt.client;

import org.agilar.gwt.sortabletable.PagedTableModel;
import org.agilar.gwt.sortabletable.PagedTableModelSource;

public class SamplePagedTableModel extends PagedTableModelSource
{
	private static final int ROW_COUNT = 27; 
	
	public Character getColumn1(int row, boolean ascending)
	{
		if (ascending)
		{
			return Character.valueOf((char)((int)'A' + ROW_COUNT - 1 - row));
		}
		else
		{
			return Character.valueOf((char)((int)'A' + row));
		}
	}
	
	public Integer getColumn2(int row, boolean ascending)
	{
		if (ascending)
		{
			return row;
		}
		else
		{
			return ROW_COUNT - row - 1;
		}
	}
	
	public Integer getColumn3(int row, boolean ascending)
	{
		if (ascending)
		{
			return (ROW_COUNT - row - 1) * 3;
		}
		else
		{
			return row * 3;
		}
	}

	private int getPageCount(int pageSize)
	{
		return ROW_COUNT / pageSize + (ROW_COUNT % pageSize > 0 ? 1 : 0);
	}

	@Override
	public void getData(int page, int sortColumn, boolean ascending,
			int pageSize, PagedTableModel model)
	{
		if (page >= getPageCount(pageSize))
		{
			page = getPageCount(pageSize)-1;
		}
		
		if (page < 0)
		{
			page = 0;
		}
		
		int j=0;
		int start = page*pageSize;
		int end = (page+1)*pageSize;
		if (end >= ROW_COUNT)
		{
			end = ROW_COUNT;
		}
		Object [][] data = new Object[end-start][3];
		for (int i=page*pageSize; i<(page+1)*pageSize && i<ROW_COUNT; i++)
		{
			data[j][0] = getColumn1(i, ascending);
			data[j][1] = getColumn2(i, ascending);
			data[j][2] = getColumn3(i, ascending);
			j++;
		}
		
		model.setData(page, data, getPageCount(pageSize), ROW_COUNT);
	}
}
